package com.elinkway.infinitemovies.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;

public class MyHorizontalScrollView extends HorizontalScrollView {

	ImageView _bt_left, _bt_right;

	private int currentPos;

	private int totalPos;

	private int screenWidth;

	public void setScreenWidth(int screenWidth) {
		this.screenWidth = screenWidth;
		currentPos = computeHorizontalScrollOffset();
		totalPos = computeHorizontalScrollRange();
	}

	public MyHorizontalScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public void computeScroll() {

		currentPos = computeHorizontalScrollOffset();
		totalPos = computeHorizontalScrollRange();
		if (totalPos <= screenWidth) {
			if (_bt_left != null) {
				_bt_left.setVisibility(View.INVISIBLE);
			}
			if (_bt_right != null) {
				_bt_right.setVisibility(View.INVISIBLE);
			}
		} else {
			if (currentPos == 0) {
				if (_bt_left != null) {
					_bt_left.setVisibility(View.INVISIBLE);
				}
				if (_bt_right != null) {
					_bt_right.setVisibility(View.VISIBLE);
				}
			}
			if (currentPos > 0 & currentPos < totalPos - screenWidth) {
				if (_bt_left != null) {
					_bt_left.setVisibility(View.VISIBLE);
				}
				if (_bt_right != null) {
					_bt_right.setVisibility(View.VISIBLE);
				}
			}
			if (currentPos >= totalPos - screenWidth) {
				if (_bt_left != null) {
					_bt_left.setVisibility(View.VISIBLE);
				}
				if (_bt_right != null) {
					_bt_right.setVisibility(View.INVISIBLE);
				}
			}
		}
		super.computeScroll();
	}

	public void setLeftButton(ImageView bt1) {
		_bt_left = bt1;
	}

	public void setRightButton(ImageView bt2) {
		_bt_right = bt2;
	}

	public void scrollToCurrent(int index, int count) {
		if (index < count && count > 0) {
			// smoothScrollTo(currentPos + (index < 0 ? 0 : index) * (totalPos /
			// count), 0);
			scrollBy(currentPos + (index < 0 ? 0 : index) * (totalPos / count),
					0);
		}
	}
}
